package com.robot.goods.service;

import java.time.LocalDateTime;
import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.robot.goods.dao.GoodsDao;
import com.robot.goods.pojo.Goods;

/**
 * goods服务层
 *
 * @author wuyunbin
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Goods> findAll() {
        return goodsDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findSearch(Map whereMap, int page, int size) {
        Specification<Goods> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return goodsDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Goods> findSearch(Map whereMap) {
        Specification<Goods> specification = createSpecification(whereMap);
        return goodsDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Goods findById(String id) {
        return goodsDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param goods
     */
    public void add(Goods goods) {
        //雪花分布式ID生成器
        goods.setId(idWorker.nextId() + "");
        LocalDateTime now = LocalDateTime.now();
        goods.setCreatedAt(now);
        goods.setUpdatedAt(now);
        goodsDao.save(goods);
    }

    /**
     * 修改
     *
     * @param goods
     */
    public void update(Goods goods) {
        goods.setUpdatedAt(LocalDateTime.now());
        goodsDao.save(goods);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        goodsDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Goods> createSpecification(Map searchMap) {
        return new Specification<Goods>() {
            @Override
            public Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 商品分类
                String categoryId = "categoryId";
                if (searchMap.get(categoryId) != null && !"".equals(searchMap.get(categoryId))) {
                    predicateList.add(cb.like(root.get(categoryId).as(String.class), "%" + (String) searchMap.get(categoryId) + "%"));
                }
                // 店铺id
                String storeId = "storeId";
                if (searchMap.get(storeId) != null && !"".equals(searchMap.get(storeId))) {
                    predicateList.add(cb.like(root.get(storeId).as(String.class), "%" + (String) searchMap.get(storeId) + "%"));
                }
                // 商品名称
                String name = "name";
                if (searchMap.get(name) != null && !"".equals(searchMap.get(name))) {
                    predicateList.add(cb.like(root.get(name).as(String.class), "%" + (String) searchMap.get(name) + "%"));
                }
                // 图片(json值)
                String images = "images";
                if (searchMap.get(images) != null && !"".equals(searchMap.get(images))) {
                    predicateList.add(cb.like(root.get(images).as(String.class), "%" + (String) searchMap.get(images) + "%"));
                }
                // 是否plus会员
                String isCoupon = "isCoupon";
                if (searchMap.get(isCoupon) != null && !"".equals(searchMap.get(isCoupon))) {
                    predicateList.add(cb.like(root.get(isCoupon).as(String.class), "%" + (String) searchMap.get(isCoupon) + "%"));
                }
                // 上下架状态 0-下架 1-上架
                String onSale = "onSale";
                if (searchMap.get(onSale) != null && !"".equals(searchMap.get(onSale))) {
                    predicateList.add(cb.like(root.get(onSale).as(String.class), "%" + (String) searchMap.get(onSale) + "%"));
                }
                // 审核状态  0-待审核 1-审核通过 2-审核未通过
                String status = "status";
                if (searchMap.get(status) != null && !"".equals(searchMap.get(status))) {
                    predicateList.add(cb.like(root.get(status).as(String.class), "%" + (String) searchMap.get(status) + "%"));
                }
                // 介绍
                String introduction = "introduction";
                if (searchMap.get(introduction) != null && !"".equals(searchMap.get(introduction))) {
                    predicateList.add(cb.like(root.get(introduction).as(String.class), "%" + (String) searchMap.get(introduction) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(Date.class)));

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 通过categoryId查询goods列表并分页
     *
     * @param categoryId
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findAllByCategoryIdAndDeletedAtIsNullOrderByCreatedAtDesc(String categoryId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return goodsDao.findAllByCategoryIdAndDeletedAtIsNullOrderByCreatedAtDesc(categoryId, pageable);
    }

    /**
     * 通过categoryId和storeId查询goods列表并分页
     *
     * @param categoryId
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findAllByCategoryIdAndStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(String categoryId, String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return goodsDao.findAllByCategoryIdAndStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(categoryId, storeId, pageable);
    }

    /**
     * 通过storeId查询goods列表并分页
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findAllByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(String storeId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return goodsDao.findAllByStoreIdAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, pageable);
    }


    /**
     * 通过categoryId,onSale,status查询goods列表并分页
     *
     * @param categoryId
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findAllByCategoryIdAndOnSaleAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(String categoryId, String onSale, String status, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return goodsDao.findAllByCategoryIdAndOnSaleAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(categoryId, onSale, status, pageable);
    }

    /**
     * 通过storeId,onSale,status查询goods列表并分页
     *
     * @param storeId
     * @param page
     * @param size
     * @return
     */
    public Page<Goods> findAllByStoreIdAndOnSaleAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(String storeId, String onSale, String status, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return goodsDao.findAllByStoreIdAndOnSaleAndStatusAndDeletedAtIsNullOrderByCreatedAtDesc(storeId, onSale, status, pageable);
    }

    /**
     * 通过id和storeId查询goods
     *
     * @param id
     * @param storeId
     * @return
     */
    public Goods findByIdAndStoreIdAndDeletedAtIsNull(String id, String storeId) {
        return goodsDao.findByIdAndStoreIdAndDeletedAtIsNull(id, storeId);
    }

}
